package com.bocai.kelalacar.ui.car.set.act;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bocai.kelalacar.App;
import com.bocai.kelalacar.R;
import com.bocai.kelalacar.base.BaseActivity;
import com.bocai.kelalacar.bean.ApplyWayBean;
import com.bocai.kelalacar.ui.car.main.act.MainActivity;
import com.bocai.kelalacar.ui.car.set.contract.WithdrawTypeContract;
import com.bocai.kelalacar.ui.car.set.module.WithdrawTypeModule;
import com.bocai.kelalacar.ui.car.set.presenter.WithdrawTypePresenter;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by yuanfei on 2017/6/22.
 */

public class WithdrawTypeActivity extends BaseActivity implements WithdrawTypeContract.view {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.right_btn)
    Button rightBtn;
    @BindView(R.id.toolbar)
    Toolbar toolbar;
    @BindView(R.id.tv_type)
    TextView tvType;
    @BindView(R.id.tv_alipay)
    TextView tvAlipay;
    @BindView(R.id.ll_alipay)
    LinearLayout llAlipay;
    @BindView(R.id.tv_wechat)
    TextView tvWechat;
    @BindView(R.id.ll_wechat)
    LinearLayout llWechat;
    @BindView(R.id.tv_union_pay)
    TextView tvUnionPay;
    @BindView(R.id.ll_union_pay)
    LinearLayout llUnionPay;
    @Inject
    WithdrawTypePresenter presenter;
    boolean alipaySet = false;
    boolean wechatSet = false;
    boolean unionpaySet = false;
    private ApplyWayBean bean;
    @BindView(R.id.ll_back)
    LinearLayout llBack;

    @Override
    public int getLayoutId() {
        return R.layout.act_withdraw_type;
    }

    @Override
    public void initPresenter() {
        App.get(this).getAppComponent().addSub(new WithdrawTypeModule(this)).inject(this);
    }

    @Override
    public void initView() {
        initToolbar("提现方式");
        initBack(imgBack,llBack);
        presenter.applyWay();
    }

    @OnClick({R.id.ll_alipay,R.id.ll_wechat,R.id.ll_union_pay})
    public void onClick(View view){
        switch (view.getId()){
            case R.id.ll_alipay:
                Intent intent = new Intent(WithdrawTypeActivity.this,AddWithdrawTypeActivity.class);
                intent.putExtra("index",1);
                if (alipaySet){
                    intent.putExtra("account",bean.getAlipay().getAccount());
                }
                startActivityForResult(intent,11);
                break;
            case R.id.ll_wechat:
                Intent intent1 = new Intent(WithdrawTypeActivity.this,AddWithdrawTypeActivity.class);
                intent1.putExtra("index",2);
                if (wechatSet){
                    intent1.putExtra("account",bean.getWeixin().getAccount());
                }
                startActivityForResult(intent1,11);
                break;
            case R.id.ll_union_pay:
                Intent intent2 = new Intent(WithdrawTypeActivity.this,UnionPayManageActivity.class);
//                if (unionpaySet){
//                    intent2.putParcelableArrayListExtra("data",bean.getBankCard());
//                }
                startActivityForResult(intent2,11);
                break;
        }
    }

    @Override
    public void applyWaySuccess(ApplyWayBean bean) {
        this.bean = bean;
        if (bean.getAlipay()!=null&&bean.getAlipay().getWayId()!=null){
            tvAlipay.setText("修改");
            alipaySet = true;
        }else {
            alipaySet = false;
            tvAlipay.setText("未设置");
        }
        if (bean.getWeixin()!=null&&bean.getWeixin().getWayId()!=null){
            tvWechat.setText("修改");
            wechatSet = true;
        }else {
            wechatSet = false;
            tvWechat.setText("未设置");
        }
        if (bean.getBankCard()!=null&&bean.getBankCard().size()!=0){
            tvUnionPay.setText("已绑定"+bean.getBankCard().size()+"张卡");
            unionpaySet = true;
        }else {
            unionpaySet = false;
            tvUnionPay.setText("未设置");
        }
    }

    @Override
    public void addApplyWaySuccess() {

    }

    @Override
    public void deleteApplyWaySuccess(int index) {

    }

    @Override
    public void walletApplySuccess() {

    }

    @Override
    public void showLoading(String title) {
        startProgressDialog(title);
    }

    @Override
    public void stopLoading() {
        stopProgressDialog();
    }

    @Override
    public void showErrorTip(String msg) {
        showShortToast(msg);
        if (msg.equals("您的登录已失效，请重新登录")){
            startActivity(new Intent(this,MainActivity.class).putExtra("index",1));
            finish();
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 11&&resultCode == 19){
            presenter.applyWay();
        }
    }
}
